package com.school.controller.student;

import com.school.service.StudentService;
import com.school.service.impl.StudentServiceImpl;
import com.school.util.ResponseJsonUtil;
import com.school.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@WebServlet("/student/deletes")
public class DeletesController extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端提交的多个id
        String[] id = req.getParameterValues("id");// ID
        System.out.println(Arrays.toString(id));
        if (id != null) {
            List<Integer> ids = new LinkedList<>();

            for (int i = 0; i < id.length; i++) {
                ids.add(Integer.valueOf(id[i]));
            }
            // 调用Service中的方法
            studentService.delete(ids);
            ResponseJsonUtil.write(resp, R.ok());
        } else {
            // json格式传递到前端
            ResponseJsonUtil.write(resp, R.err());
        }
    }
}
